package com.sun.user.controller;


import com.sun.common.authoriza.util.CurrentUserUtils;
import com.sun.common.result.ActionResult;
import com.sun.common.model.UserModel;
import com.sun.common.result.SupportResult;
import com.sun.common.service.UserService;
import com.sun.common.util.SequenceGenerator;
import com.sun.user.model.UserGroupModel;
import com.sun.user.service.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.*;

//用户登录操作

@RestController
@RequestMapping("/adminUser")
public class AdminUserController {

    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    private static SequenceGenerator sequenceGenerator = new SequenceGenerator();

    @Autowired
    private UserService userService;

    @Autowired
    private UserGroupService userGroupService;

    @GetMapping(value = "/userInfo")
    public SupportResult userInfo() {
        Map<String, Object> currentUser = CurrentUserUtils.getOAuth2AuthenticationDetailsInfo();
        Map<String, Object> re = new HashMap<>();
        re.put("permissions", currentUser.get("authorities"));
        re.put("username", ((UserModel) currentUser.get("user")).getAccount());
        re.put("avatar", ((UserModel) currentUser.get("user")).getAvatar());
        return new SupportResult(re);
    }

    @PostMapping("/register")
    public ActionResult  Register(@RequestParam("password") String password,@RequestParam("name") String account){
        UserModel userModel=new UserModel();
        userModel.setId(sequenceGenerator.nextStringId());
        userModel.setCompany("0");
        userModel.setType("inside");
        userModel.setPassword(encoder.encode(password));
        userModel.setAccount(account);
        userModel.setAvatar("https://i.gtimg.cn/club/item/face/img/2/15922_100.gif");
        userService.save(userModel);
        UserGroupModel userGroupModel=new UserGroupModel();
        userGroupModel.setAccount(account);
        userGroupModel.setGroup("2");
        userGroupModel.setProject("1");
        userGroupService.save(userGroupModel);
        return ActionResult.success();
    }




//    @GetMapping("/login")
////    public ActionResult  login(@RequestBody UserModel userModel){
//    public ActionResult  login(){
//        UserModel userModel=new UserModel();
//        userModel.setAccount("maanger");
//        userModel.setPassword("111111");
//        UserModel uInfo=userService.lambdaQuery().eq(UserModel::getAccount,userModel.getAccount()).one();
//        System.out.println("[BCryptPasswordEncoder]"+ (new BCryptPasswordEncoder()).encode("123456"));
//
//        if(uInfo!=null&&StrUtil.equals(uInfo.getPassword(),userModel.getPassword())){
//            return ActionResult.SUCCESS();
//        }else{
//            return ActionResult.FAIL();
//        }
//
//    }


//    @GetMapping("/profile")
//    public ActionResult  profile(){
//
//        return ActionResult.SUCCESS();
//    }




}
